<?php

/**
 * Erra_Fields_Search_ComboBox - Search combobox
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Erra_Fields_Search_ComboBox extends Erra_Fields_Base_Field {

    protected $_operator;

    public function __construct($objectId) {
        parent::__construct($objectId);
        $this->setDatabaseType('none');
    }

    /**
     * Edit combobox search field
     *
     * @param mixed $lang
     * @return
     */
    public function editInstance(Array $extraProperties=null) {
        $res = '<div class="erra-form-field-instance">';
        $res .= $this->outputFieldHeader();
        
        $res .= '<select name="' . $this->getHtmlName() . '[criteria]" id="' . $this->getHtmlId() . '_criteria">';
        foreach ($this->fieldData as $optionKey => $optionValue) {
            $selected_flag = (!is_null($this->_rawData) && $this->_rawData == $optionKey) ? ' selected ' : '';
            $res .= '<option ' . $selected_flag . ' value="' . $optionKey . '">' . $optionValue . '</option>';
        }
        $res .= '</select>';
        $res .= '<input '.$this->getHtmlAttributes($extraProperties).' type="hidden" name="' . $this->getHtmlName() . '[operator]" id="' . $this->getHtmlId() . '_operator" value="' . $this->getOperator() . '" />';
        $res .= '</div>';
        return $res;
    }

    function toHtml($displayParameters = null) {
        if (isset($this->fieldData) && is_array($this->fieldData) && isset($this->fieldData[$this->getRawData()])) {
            return $this->fieldData[$this->getRawData()];
        }
        return $this->getRawData();
    }

    /** Getters and setters */
    public function setOperator($operator) {
        $this->_operator = $operator;
        return $this;
    }

    public function getOperator() {
        return $this->_operator;
    }

}